/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.backup;

import com.blandware.atleap.common.Constants;
import com.blandware.atleap.common.util.DateUtil;
import com.blandware.atleap.service.core.BackupManager;
import com.blandware.atleap.service.core.ContentLocaleManager;
import com.blandware.atleap.service.core.MailTemplateManager;
import com.blandware.atleap.webapp.util.core.*;
import com.blandware.atleap.webapp.menu.MenuRepository;
import com.blandware.atleap.search.SearchManager;
import org.apache.struts.action.*;
import org.apache.struts.util.RequestUtils;
import org.springframework.dao.DataAccessException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

/**
 * <p>This action restore database data from some backup archive</p>
 * <p/>
 * <p><a href="RestoreAction.java.html"><i>View Source</i></a></p>
 *
 * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
 * @version $Revision: 1.6 $ $Date: 2007/06/06 13:43:02 $
 * @struts.action path="/core/backup/restore"
 * validate="false"
 * roles="core-backup-restore"
 * @struts.action-forward name="listArchives"
 * path="/core/backup/list.do"
 * redirect="true"
 * @struts.action-forward name="inputForward"
 * path="/core/backup/list.do"
 * @struts.action-forward name="unsatisfiable"
 * path="/core/backup/list.do"
 */
public class RestoreAction extends RestoreActionSupport {
    /**
     * @param mapping  The ActionMapping used to select this instance
     * @param form     The optional ActionForm bean for this request (if any)
     * @param request  The HTTP request we are proceeding
     * @param response The HTTP response we are creating
     * @return an ActionForward instance describing where and how
     *         control should be forwarded, or null if response
     *         has already been completed
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) throws Exception {

        String dateString = request.getParameter("date");

        Date date = null;
        if (dateString != null) {
            try {
                Locale locale = RequestUtils.getUserLocale(request, null);
                date = DateUtil.parseDateTime(dateString, locale);
            } catch(ParseException ex) {
                ActionMessages errors = new ActionMessages();
                errors.add("cannotRestore", new ActionMessage("core.backup.errors.cannotRestore"));
                saveErrors(request, errors);
                return mapping.findForward("inputForward");
            }
        }

        BackupManager backupManager = (BackupManager) getBean(Constants.BACKUP_MANAGER_BEAN);
        try {
            backupManager.restore(date, Boolean.TRUE);
        } catch (DataAccessException ex) {
            ActionMessages errors = new ActionMessages();
            errors.add("cannotRestore", new ActionMessage("core.backup.errors.cannotRestore"));
            saveErrors(request, errors);
            return mapping.findForward("inputForward");
        }

        doAfterRestore(request);

        ActionMessages messages = new ActionMessages();
        messages.add("success", new ActionMessage("core.backup.messages.success"));
        saveMessages(request, messages);

        return mapping.findForward("listArchives");
    }
}
